<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item prop="phone" label="手机号">
          <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker
            v-model="formInline.dates"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="reset">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="用户ID"> </el-table-column>
      <el-table-column label="头像">
        <template slot-scope="scope">
          <el-avatar :size="'small'" :src="scope.row.portrait"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column label="注册时间">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div
            :class="{
              enable: scope.row.status === 'ENABLE',
              disable: scope.row.status !== 'ENABLE'
            }"
          ></div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="click1(scope.row)" type="text" size="small"
            >分配角色</el-button
          >
          <el-button
            v-if="scope.row.status === 'ENABLE'"
            @click="click2(scope.row)"
            type="text"
            size="small"
            >禁用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:20px;display:flex;justify-content:center;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="分配角色" :visible.sync="dialogVisible" width="50%">
      <el-select v-model="value1" multiple placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modelCancel">取 消</el-button>
        <el-button type="primary" @click="modelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
import {
  getUserPages,
  forbidUser,
  getUserRoleMenuLists,
  allocateUserRoles,
  getRolesByUserId
} from './api/index'
import dayjs from 'dayjs'
export default Vue.extend({
  name: 'UserIndex',
  data () {
    return {
      formInline: {
        dates: [],
        currentPage: 1,
        pageSize: 5,
        phone: '',
        userId: 0,
        startCreateTime: '',
        endCreateTime: ''
      },
      currentPage4: 4,
      tableData: [],
      total: 0,
      dialogVisible: false,
      options: [
        {
          value: '选项1',
          label: '黄金糕'
        }
      ],
      value1: [],
      row: null
    }
  },
  // async created () {

  // },
  methods: {
    onSubmit () {
      console.log(this.formInline)
      if (this.formInline.currentPage !== 1) {
        this.formInline.currentPage = 1
      } else {
        this.getUserPages()
      }
    },
    async getUserPages () {
      const { data } = await getUserPages(this.formInline)
      console.log(data)
      this.tableData = data.data.records
      this.total = data.data.total
    },
    async click1 (row) {
      console.log(row)
      this.row = row
      await this.getUserRoleMenuLists()
      await this.getRolesByUserId(row.id)
      this.dialogVisible = true
    },
    async click2 (row) {
      const { data } = await forbidUser(row.id)
      if (data.code === '000000') {
        this.$message.success('操作成功')
        await this.getUserPages()
      }
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.formInline.pageSize = val
      if (this.formInline.currentPage !== 1) {
        this.formInline.currentPage = 1
      } else {
        this.getUserPages()
      }
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.formInline.currentPage = val
    },
    reset () {
      if (this.formInline.currentPage !== 1) {
        this.formInline = {
          dates: [],
          currentPage: 1,
          pageSize: 5,
          phone: '',
          userId: 0,
          startCreateTime: '',
          endCreateTime: ''
        }
      } else {
        this.formInline = {
          dates: [],
          currentPage: 1,
          pageSize: 5,
          phone: '',
          userId: 0,
          startCreateTime: '',
          endCreateTime: ''
        }
        this.getUserPages()
      }
    },
    async modelCancel () {
      this.dialogVisible = false
      this.value1 = []
    },
    async modelConfirm () {
      if (this.value1?.length === 0) {
        this.$message.error('请选择角色')
        return
      }

      console.log(this.value1)
      await allocateUserRoles({
        userId: this.row.id,
        roleIdList: this.value1
      })
      this.$message.success('操作成功')
      this.dialogVisible = false
    },
    async getUserRoleMenuLists () {
      const res = await getUserRoleMenuLists()
      console.log(res)
      this.options = res.data.data
    },
    async getRolesByUserId (id: number) {
      const res = await getRolesByUserId(id)
      console.log(res)
      this.value1 = res.data.data.map(ele => {
        return ele.id
      })
    }
  },
  watch: {
    'formInline.currentPage': {
      immediate: true,
      async handler (n, o) {
        console.log(n, o)
        await this.getUserPages()
      }
    },
    'formInline.dates': {
      immediate: true,
      async handler (n) {
        if (n && n.length && n.length > 0) {
          this.formInline.startCreateTime = dayjs(n[0]).format('YYYY-MM-DD')
          this.formInline.endCreateTime = dayjs(n[1]).format('YYYY-MM-DD')
        }
      }
    }
  }
})
</script>

<style lang="scss" scoped>
.enable {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: green;
}
.disable {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
}
</style>
